package com.kinon.tyjaccount.adapter;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.tyjaccount.R;
import com.kinon.tyjaccount.sqlitebean.SettletmentBean;
import com.kinon.tyjaccount.tools.HttpTools;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

/**订单item*/

public class UrgentDingdanAdapter extends BaseAdapter{
	Context context;
	ArrayList<SettletmentBean> list;
	HttpTools httpTools=null;
	View iscatview = null;

	public UrgentDingdanAdapter(Context context, ArrayList<SettletmentBean> list) {
		super();
		this.context = context;
		this.list = list;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHandle handle;
		if(view==null){
			handle=new ViewHandle();
			view=LayoutInflater.from(context).inflate(R.layout.item_dingdan, null);
			handle.tv_bianhao=(TextView)view.findViewById(R.id.tv_order);
			handle.tv_name=(TextView)view.findViewById(R.id.tv_name);
			handle.tv_jine=(TextView)view.findViewById(R.id.tv_jine);
			handle.tv_shijian=(TextView)view.findViewById(R.id.tv_time);
			handle.btn_tuikuan=(Button)view.findViewById(R.id.btn_tuikuan);
			handle.btn_tuikuanyiwancheng=(Button)view.findViewById(R.id.btn_tuikuanyiwancheng);
			view.setTag(handle);
		}else {
			handle = (ViewHandle) view.getTag();
		}
		if (list.get(position).getRefund()==0) {
			handle.btn_tuikuan.setVisibility(View.VISIBLE);
			handle.btn_tuikuan.setText("申请退款");
			handle.btn_tuikuan.setClickable(true);
			handle.btn_tuikuanyiwancheng.setVisibility(View.GONE);

		}else {
			handle.btn_tuikuan.setVisibility(View.VISIBLE);
			handle.btn_tuikuan.setText("退款处理中");
			handle.btn_tuikuan.setClickable(false);
//			handle.btn_tuikuan.setText("退款已完成");
//			handle.btn_tuikuan.setEnabled(false);
//			handle.btn_tuikuan.setVisibility(View.GONE);
//			handle.btn_tuikuanyiwancheng.setVisibility(View.VISIBLE);
		}
		handle.tv_bianhao.setText(list.get(position).getOrderno());
		handle.tv_name.setText(list.get(position).getCard());
		handle.tv_jine.setText("￥"+list.get(position).getTotal());
		handle.tv_shijian.setText(list.get(position).getTime());
		handle.btn_tuikuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tuikuandialog(position);
			}
		});

		return view;
	}
	class ViewHandle {
		/**订单编号*/
		TextView tv_bianhao;
		/**姓名*/
		TextView tv_name;
		/**价格*/
		TextView tv_jine;
		/**时间*/
		TextView tv_shijian;
		/**退款*/
		Button btn_tuikuan;
		/**退款*/
		Button btn_tuikuanyiwancheng;
	}

	/**支付弹窗*/
	private void tuikuandialog(final int index2) {

		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		iscatview = LayoutInflater.from(context).inflate(
				R.layout.dialog_pay, null);
		TextView tital = (TextView) iscatview.findViewById(R.id.tital);
		TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
		TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
		tital.setText("是否立即退款?");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		tv_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Tuikuan(index2);
				dialog.dismiss();
			}
		});
	}
	private void Tuikuan(final int position) {
		String id=list.get(position).getOrderno();
		list.get(position).setRefund(1);
		ContentValues values=new ContentValues();
		values.put("refund",1);
		DataSupport.updateAll(SettletmentBean.class,values,"orderno=?",id);
		notifyDataSetChanged();
	}


}
